﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using CrystalDecisions.CrystalReports.Engine;

using BLL;
using DAL;
using Models;
using System.Data;
using System.Text;
using System.Xml;
using Web.Old_App_Code;

public partial class backstage_user_rank : System.Web.UI.Page
{
    // Fields

    protected UserRank ur = new UserRank();
    protected UserRankManager urm = new UserRankManager();

    // Methods
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string pathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
        try
        {
            this.ur.RankName = this.txtRankName.Value;
            this.ur.Points = Convert.ToInt32(this.txtPoints.Value);
            this.ur.Discount = Convert.ToInt32(this.txtDiscount.Value);
            this.ur.IsDefault = Convert.ToInt32(base.Request.Form["isDefault"]);
            this.ur.LanguageVersion = this.Session["language_version"].ToString();
            if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
            {
                this.ur.Id = Convert.ToInt32(base.Request.QueryString["id"]);
                if (this.urm.ModifyUserRank(this.ur))
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新成功！\");location.href=\"user_rank_list.aspx\";</script>");
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");</script>");
                }
            }
            else if (this.urm.AddUserRank(this.ur))
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加成功！\");location.href=\"user_rank_list.aspx\";</script>");
            }
            else
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加失败！\");</script>");
            }
        }
        catch
        {
            if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");</script>");
            }
            else
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加失败！\");</script>");
            }
        }
    }

    public StringBuilder GetTitle()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            if (base.Request.QueryString["ot"] == null)
            {
                return builder;
            }
            if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
            {
                builder.Append("编辑等级");
                return builder;
            }
            builder.Append("添加等级");
        }
        catch
        {
        }
        return builder;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if ((HttpContext.Current.Session["admin_user"] == null) && (HttpContext.Current.Session["language_version"] == null))
        {
            base.ClientScript.RegisterStartupScript(base.GetType(), "sJs", "top.location=\"login.html\";", true);
        }
        if (!base.IsPostBack && (base.Request.QueryString["ot"] != null))
        {
            if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
            {
                this.UserRankBind();
            }
            else
            {
                this.rdIsDefault0.Checked = true;
            }
        }
    }

    private void UserRankBind()
    {
        try
        {
            DataTable userRankById = this.urm.GetUserRankById(Convert.ToInt32(base.Request.QueryString["id"]));
            if (userRankById.Rows.Count > 0)
            {
                this.txtRankName.Value = userRankById.Rows[0]["rankName"].ToString();
                this.txtPoints.Value = userRankById.Rows[0]["points"].ToString();
                this.txtDiscount.Value = userRankById.Rows[0]["discount"].ToString();
                if (Convert.ToInt32(userRankById.Rows[0]["isDefault"]) == 1)
                {
                    this.rdIsDefault1.Checked = true;
                }
                else
                {
                    this.rdIsDefault0.Checked = true;
                }
            }
        }
        catch
        {
        }
    }

}